package com.hivekion.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hivekion.common.entity.TreeNode;
import com.hivekion.common.exception.BusinessException;
import com.hivekion.system.domain.SysDictType;
import com.hivekion.system.domain.vo.SysDictTypeCreateVo;
import com.hivekion.system.domain.vo.SysDictTypeModelVo;
import com.hivekion.system.domain.vo.SysDictTypeUpdateVo;
import com.hivekion.system.domain.vo.SysDictTypeViewVo;
import com.hivekion.system.mapper.SysDictTypeMapper;
import com.hivekion.system.service.ISysDictTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysDictTypeService implements ISysDictTypeService {
      @Autowired
      SysDictTypeMapper dictTypeMapper;

      @Override
      public Boolean checkTypeNameUnique(String typeName, String key) {
	    LambdaQueryWrapper<SysDictType> queryWrapper = Wrappers.lambdaQuery();
	    queryWrapper.eq(SysDictType::getDictName, typeName);
	    if (StringUtils.isNotEmpty(key)) {
		  queryWrapper.ne(SysDictType::getId, key);
	    }
	    List<SysDictType> list = dictTypeMapper.selectList(queryWrapper);
	    return list.size() > 0;
      }

      @Override
      public Boolean checkTypeCodeUnique(String typeCode, String key) {
	    LambdaQueryWrapper<SysDictType> queryWrapper = Wrappers.lambdaQuery();
	    queryWrapper.eq(SysDictType::getDictCode, typeCode);
	    if (StringUtils.isNotEmpty(key)) {
		  queryWrapper.ne(SysDictType::getId, key);
	    }
	    List<SysDictType> list = dictTypeMapper.selectList(queryWrapper);
	    return list.size() > 0;
      }

      @Override
      public boolean add(SysDictTypeCreateVo createVo) {
	    if (this.checkTypeNameUnique(createVo.getDictName(), "")) {
		  throw new BusinessException(500, "分类名称已存在，请重新输入！");
	    }
	    if (this.checkTypeCodeUnique(createVo.getDictCode(), "")) {
		  throw new BusinessException(500, "分类编号已存在，请重新输入！");
	    }
	    SysDictType dictType = new SysDictType();
	    BeanUtils.copyProperties(createVo, dictType);
	    return dictTypeMapper.insert(dictType) > 0;
      }

      @Override
      public boolean edit(SysDictTypeUpdateVo updateVo) {
	    if (this.checkTypeNameUnique(updateVo.getDictName(), updateVo.getId())) {
		  throw new BusinessException(500, "分类名称已存在，请重新输入！");
	    }
	    if (this.checkTypeCodeUnique(updateVo.getDictCode(), updateVo.getId())) {
		  throw new BusinessException(500, "分类编号已存在，请重新输入！");
	    }
	    SysDictType dictType = dictTypeMapper.selectById(updateVo.getId());
	    BeanUtils.copyProperties(updateVo, dictType);
	    return dictTypeMapper.updateById(dictType) > 0;
      }

      @Override
      public boolean remove(String key) {
	    return dictTypeMapper.deleteById(key) > 0;
      }

      @Override
      public SysDictTypeModelVo get(String key) {
	    SysDictType dictType = dictTypeMapper.selectById(key);
	    SysDictTypeModelVo vo = new SysDictTypeModelVo();
	    BeanUtils.copyProperties(dictType, vo);
	    return vo;
      }

      @Override
      public SysDictTypeModelVo getByCode(String code) {
	    LambdaQueryWrapper<SysDictType> queryWrapper = Wrappers.lambdaQuery();
	    queryWrapper.eq(SysDictType::getDictCode, code);
	    SysDictType dictType = dictTypeMapper.selectOne(queryWrapper);
	    SysDictTypeModelVo vo = new SysDictTypeModelVo();
	    BeanUtils.copyProperties(dictType, vo);
	    return vo;
      }

      @Override
      public List<SysDictTypeViewVo> getAll() {
	    return dictTypeMapper.selectList(null).stream()
		    .map(
			    item -> {
				  SysDictTypeViewVo vo = new SysDictTypeViewVo();
				  BeanUtils.copyProperties(item, vo);
				  return vo;
			    })
		    .collect(Collectors.toList());
      }

      @Override
      public List<TreeNode> getTreeNodes() {
	    List<TreeNode> nodes =
		    dictTypeMapper.selectList(null).stream()
			    .sorted(Comparator.comparing(SysDictType::getSortCode))
			    .map(
				    item -> {
					  TreeNode node = new TreeNode();
					  node.setChildren(null);
					  node.setLeaf(true);
					  node.setValue(item.getId());
					  node.setTitle(
						  item.getDictName() + "(" + item.getDictCode() + ")");
					  node.setKey(item.getDictCode());
					  return node;
				    })
			    .collect(Collectors.toList());
	    return nodes;
      }
}
